package com.gome.action;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.gome.util.NoticeUtil;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.MessageType;

/**
 * @author wangaoyun
 * @date 2021/4/13
 */
public class WeatherAction extends AnAction {

    private static final String WEATHER_API = "http://api.jirengu.com/getWeather.php";

    @Override
    public void actionPerformed(AnActionEvent e) {
        String weatherContent = getWeather();
        NoticeUtil.noticeInfo(weatherContent);
    }

    private String getWeather() {
        String pattern = "province:{}, weather:{}, temperature:{}";

        String response = HttpUtil.get(WEATHER_API, CharsetUtil.CHARSET_UTF_8);
        JSON responseObj = JSONUtil.parse(response);
        String province = responseObj.getByPath("result.location.province", String.class);
        String weather = responseObj.getByPath("result.now.text", String.class);
        String temperature = responseObj.getByPath("result.now.temp", String.class);

        return StrUtil.format(pattern, province, weather, temperature);
    }
}
